package com.ykdl.app.base;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.ykdl.app.R;
import com.ykdl.app.base.BaseFragment.onNetWorkNoConnectListener;


public abstract class WebViewActivity extends BaseFragmentActivity implements OnClickListener {
	
	public  WebView webView;
//	private ProgressBar progressbar;
	private Activity  activity;
	
	
	private String title_string ="";
	private Boolean isfirst=true;
	private Boolean isShowTtitle=true;
	public void setIsfirst(Boolean isfirst) {
		this.isfirst = isfirst;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		showProgress(this);
		changeSystemBarToWhite();
		getIntentData();
		SetContentLayout(R.layout.webview_activity);
		this.activity = this;
//		progressbar = (ProgressBar) this.findViewById(R.id.pb_progressbar);
		webView = (WebView) this.findViewById(R.id.webivew);
		
		setRightListener(this);
//		ib_share.setOnClickListener(this);
//		tv_title.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});
		showLoading();
		webView.setInitialScale(25);
		WebSettings  webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(true); //支持js
		webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		webView.getSettings().setUseWideViewPort(true);
		webView.getSettings().setLoadWithOverviewMode(true);
		webView.setWebChromeClient(new WebChromeClient(){
			
		});
		webView.setWebViewClient(new WebViewClient(){
			
			public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
				// TODO Auto-generated method stub
				return super.shouldOverrideKeyEvent(view, event);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
//				if (Uri.parse(url).getHost().equals("http://news.baidu.com/")) {
//		            // This is my web site, so do not override; let my WebView load the page
//		            return false;
//		        }
//		        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
//		        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//		        startActivity(intent);
				if (url.equals("hsh://exchange/goods/close")) {
					WebViewActivity.this.finish();
					return true;
					
				}else if (url.equals("hsh://store/products")) {
					WebViewActivity.this.finish();
					return true;
					
				}else if (url.contains("hsh://doctor/profile/")) {
					return true;
					
				}else if (url.contains("hys://tel/")) {
					Intent intent_phone = new Intent(Intent.ACTION_CALL,
							Uri.parse("tel:"+url.substring(10)));
					WebViewActivity.this.startActivity(intent_phone);
					return true;
					
				}else if (url.contains("hys://store/order")) {
					
					WebViewActivity.this.finish();
					return true;
					
				}else if(url.contains("hys://store/coupon")){
				
					WebViewActivity.this.finish();
					return true;
					
				}else {
					return overrideUrlLoading(view, url);
				}
				
				
			}
			
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				super.onReceivedError(view, errorCode, description, failingUrl);
				hiddenLoading();
			}
			 public void onPageFinished(WebView view,String url)
             {
				 if (isShowTtitle) {
					 String title=view.getTitle();
					 if (isfirst) {
						 title_string=title;
					}
					 isfirst=false;
					
					 if(title==null){
					 title="";
					 }
					// tv_title.setText(title); 
					 if (!title.equals("找不到网页")) {
						 setTitle(title);
					}
					 if (!title_string.equals(title)) {
						 goneButton();
					}else {
						showButton();
					}
				}
				hiddenLoading();
				
             }
			
		});
//		setTitle();
//		
//		//weiview 如果加载的页面有需要下载文件，需要设置setDownloadListener方法
//		webView.setDownloadListener(new DownloadListener() {
//			@Override
//			public void onDownloadStart(String url, String userAgent,
//					String contentDisposition, String mimetype, long contentLength) {
//				if (url != null && url.startsWith("http://")){
//                    activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
//				}
//			}
//		});
//		
		onCreateAfter();
		
		setmConnectListener(new onNetWorkNoConnectListener() {
			
			@Override
			public void onConnect(boolean isConnect) {
				
				if (isConnect) {
					startLoadUrl();
				}
			}
		});
		
	}
	public void  noShowTitle() {
		isShowTtitle=false;
	}
	public void  goneButton() {
		setRightShowOrHidden(false);
	}
	public void  showButton() {
		setRightShowOrHidden(true);
	}

	public void  setRightbt(int rightPic,
			OnClickListener onRight) {
		
		setRightBackground(rightPic);
		setRightListener(onRight);
		
	}
	protected void onCreateAfter(){
		setWapTitle();
		startLoadUrl();
	}
	protected abstract void getIntentData();
	protected abstract void setWapTitle();
	protected abstract void startLoadUrl();

	/**
	 * 用户监控内部wep页的重定向操作
	 * 如:监控特定的请求地址，实现从wap页调用本地用户
	 */
	protected abstract boolean overrideUrlLoading(WebView view, String url);


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){
			webView.goBack();//后退
//			webView.reload(); //刷新
//			webView.goForward();//前进
			return true;
		}
		return super.onKeyDown(keyCode, event); 
	}
	
	public void setTitle() {
		 WebChromeClient wvcc = new WebChromeClient() {  
	            @Override  
	            public void onReceivedTitle(WebView view, String title) {  
	                super.onReceivedTitle(view, title);  
	                title_string=title;
	              //  tv_title.setText(title_string);  
	                setTitle(title_string);
	            }  
	  
	        };  
	        // 设置setWebChromeClient对象  
	        webView.setWebChromeClient(wvcc);  
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		
		case R.id.right:
			doShare();
		default:
			break;
		}
	}
	public void doShare(){
		
	}
	
	
}
